package com.goktech.controller;

import com.goktech.entity.Book;
import com.goktech.entity.User;
import com.goktech.entity.borrow;
import com.goktech.entity.collect;
import com.goktech.service.BorrowService;
import com.goktech.service.CollectService;
import com.goktech.service.UserService;
import com.goktech.util.javaUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user/personCenter")

public class PersonCenterController {


    @Autowired
    private UserService userService;

    @Autowired
    private User u;

    @Autowired
    private borrow borrow;

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private collect c;



//    @RequestMapping("/show")
//    public String allPerson(HttpServletRequest request){
//        User byZhanghao = userService.findByZhanghao(u);
//        System.out.println(byZhanghao);
//        request.setAttribute("user",byZhanghao);
//        return "user/personCenter";
//
//
//
//    }


//    @PostMapping("/update")
//    @ResponseBody
//    public boolean updatePersonCenter(User u){
//        int count = 0;
//        System.out.println("u:"+u);
//        int byUser = userService.updateUser(u);
//        System.out.println("byUser:"+byUser);
//        if(byUser != 0){
//            count = userService.updateUser(u);
//            if(count != 0){
//                System.out.println("count:"+count);
//                return true;
//            }
//        }
//        return false;
//    }


    @RequestMapping("/")
    public String searchPerson(HttpServletRequest request){
        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        u.setZhanghao(zhanghao);
        User byZhanghao = userService.findByZhanghao(u);
        request.setAttribute("user",byZhanghao);
        System.out.println("find:"+byZhanghao);
        return "user/personCenter";
    }



    @PostMapping("/update")
    @ResponseBody
    public boolean updatePerson(User u){
        int count = 0;
        System.out.println("u:"+u);
        User byZhanghao = userService.findByZhanghao(u);
        System.out.println("byZhanghao:"+byZhanghao);
        if(byZhanghao != null){
            u.setPassword(javaUtil.getResult(u.getPassword()));
            count = userService.updateUser(u);
            if(count != 0){
                System.out.println("count:"+count);
                return true;
            }
        }
        return false;
    }


//    @PostMapping("/cancle")
//    @ResponseBody
//    public boolean canclePerson(User u){
//        System.out.println(1111111);
//        String isReturn = borrow.getIsReturn();
//        System.out.println(isReturn);
//       if(isReturn=="true"){
//           int i = userService.deleteUser(u);
//           System.out.println(i);
//           return true;
//       }
//       return false;
//
//    }


    @RequestMapping("/zhuxiao")
    public String zhuxiaoPerson(HttpServletRequest request){

        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        u.setZhanghao(zhanghao);
        User byZhanghao = userService.findByZhanghao(u);

        System.out.println("1111111111");
        List<borrow> isReturn = borrowService.findIsReturn(byZhanghao);

        request.setAttribute("user",byZhanghao);
        System.out.println(isReturn);
        if(isReturn!=null && isReturn.size() != 0){
            return "user/personCenter";
        }

        int i = userService.deleteUser(byZhanghao);
        int i1 = collectService.deleteCollect1(byZhanghao);

        if(i>0 && i1 >= 0){
            return "redirect:/login/quit";
        }

        return "user/personCenter";

//
//        System.out.println(2222222);
//
//        borrow.setUserId(userId);
//        borrow byBorrow = borrowService.findIsReturn(borrow);
//        System.out.println(byBorrow);
//        String isReturn = byBorrow.getIsReturn();
//        if(isReturn!=null && isReturn.equals("true")){
//            int i = userService.deleteUser(u);
//            int i1 = collectService.deleteCollect1(c);
//
//            System.out.println(i);
//            System.out.println(i1);
//            return false;
//        }
//        return false;
    }






//    @PostMapping("/update")
//    @ResponseBody
//    public boolean updatePerson(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        String zhanghao = (String) session.getAttribute("zhanghao");
//        int count = 0;
//        int byUser=userService.updateUser(u);
//        request.setAttribute("user",byUser);
//        System.out.println("byUser:"+byUser);
//        if(byUser != 0){
//            count = userService.updateUser(u);
//            if(count != 0){
//                System.out.println("count:"+count);
//                return true;
//            }
//        }
//        return false;
//    }





}
